<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class ReservationsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Reservations';

/**
 * Default helper
 *
 * @var array
 */
	public $helpers = array();

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Court', 'User', 'Reservation');
	
	public function isAuthorized($user) {
		if (in_array($user['role'], array('admin', 'superAdmin'))) {
			return true;
		}
		if(in_array($this->action, array('view'))) {
			if($user['username'] != $this->request->params['pass'][0]){
				return false;
			}
			return true;
		}
		if(in_array($this->action, array('add', 'jsSetAvailableHours'))) {
			return true;
		}
		return false;
	}

	public $components = array('RequestHandler');
	
	public $paginate = array('order' => array(
            'Reservation.start_time' => 'asc'
        ));
	
	public function index() {
		$this->set('title_for_layout', 'Rezerwacje - strona główna');
		$data = $this->paginate('Reservation');
		$this->set('reservations', $data);
	}
	
	public function view($username = null) {
		$this->set('title_for_layout', 'Rezerwacja');
		$data = $this->paginate('Reservation', array('User.username'=>$username));
		$this->set('reservations', $data);
	}
	
	public function add() {
		$this->set('courts', $this->Court->find('list', array(
			'fields'		=> array('Court.id', 'Court.name'),
			'conditions'	=> array('Court.out_of_order' => '0')
		)));
	}
	
	public function jsSetAvailableHours() {
		Configure::write('debug', 0);
		$this->autoRender = false;
		
		if($this->RequestHandler->isAjax()) {
			if(isset($this->params['data']['dateText']) && isset($this->params['data']['court_id'])) {
				$dateText = $this->params['data']['dateText'];
				$courtId = $this->params['data']['court_id'];
				
				$availableStartTimes = $this->Court->getAvailableStartTimes($courtId, isset($this->params['data']['isNormal']) ? $dateText : "");
				$options = array();
				foreach($availableStartTimes as $key => $value) {
					$options[substr($key, 0,5)] = $dateText." ".$value;
				}
				
				echo json_encode($options);
			}
		}
	}
}
